package client_gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import utils.Constants;
import utils.ImageCreator;
import client.ClientUser;
import client_tree.User;

/**
 * Klasa predstavlja dialog koji prikazuje informacije o nekom korisniku u kontakt listi.
 * 
 * @author UrosKrkic & Radivoje Popovic
 */
public class UserDetailsDialog extends JDialog implements Constants {

	/**
	 * Parent - prozor aplikacije.
	 */
	private ClientWindow owner;
	
	private User user;
	
	private ClientUser clientUser;
	
	private Dimension fieldDimension = new Dimension(150,20);
	
	/**
	 * LayoutManager.
	 */
	private GridBagLayout gridBag = null;
	
	
	private JPanel jContentPane = null;
	
	private JPanel detailsPanel = null;
	private JPanel buttonPanel = null;
	
	private JButton okButton = null;
	
	private GridBagConstraints avatarButtonConstraint = null;
	private GridBagConstraints usernameLabelConstraint = null;
	private GridBagConstraints usernameFieldConstraint = null;
	private GridBagConstraints nicknameLabelConstraint = null;
	private GridBagConstraints nicknameFieldConstraint = null;
	private GridBagConstraints statusLabelConstraint = null;
	private GridBagConstraints statusFieldConstraint = null;
	private GridBagConstraints emailLabelConstraint = null;
	private GridBagConstraints emailFieldConstraint = null;
	private GridBagConstraints regdateLabelConstraint = null;
	private GridBagConstraints regdateFieldConstraint = null;

	
	private JButton avatarButton = null;
	private JLabel usernameLabel = null;
	private JTextField usernameField = null;
	private JLabel nicknameLabel = null;
	private JTextField nicknameField = null;
	private JLabel statusLabel = null;
	private JTextField statusField = null;
	private JLabel emailLabel = null;
	private JTextField emailField = null;
	private JLabel regdateLabel = null;
	private JTextField regdateField = null;
	
	private Font font = new Font("Dialog", Font.BOLD, 12);
	
	
	/**
	 * Konstruktor.
	 * @param parent - prozor aplikacije
	 * @author UrosKrkic & Radivoje Popovic & Martin Jondzic
	 */
	public UserDetailsDialog(ClientWindow parent, User user) {
		super(parent, "User Details", true);
		
		this.owner = parent;
		
		this.user = user;
		
		this.gridBag = new GridBagLayout();
		
		createConstraints();
		
		initializeConstraints();
		
		initialize();
		
		setValues();
	}
	
	public UserDetailsDialog(ClientWindow parent, ClientUser clientUser) {
		super(parent, "My Details", true);
		
		this.owner = parent;
		
		this.clientUser = clientUser;
		
		this.gridBag = new GridBagLayout();
		
		createConstraints();
		
		initializeConstraints();
		
		initialize();
		
		setValues(this.clientUser);
	}
	
	
	/**
	 * Metod vrsi inicijalizaciju dialoga.
	 * @author UrosKrkic & Radivoje Popovic & Martin Jondzic
	 */
	private void initialize() {
		this.setSize(300, 250);
		this.setLocation(owner.getX() + owner.getWidth() / 2 - this.getWidth() / 2, owner.getY() + owner.getHeight() / 2 - this.getHeight() / 2);
		this.setResizable(false);
		this.setContentPane(getJContentPane());
		
		this.setIconImage(owner.getIconImage());
	}
	
	
	
	private void createConstraints() {
		avatarButtonConstraint = new GridBagConstraints();
		usernameLabelConstraint = new GridBagConstraints();
		usernameFieldConstraint = new GridBagConstraints();
		nicknameLabelConstraint = new GridBagConstraints();
		nicknameFieldConstraint = new GridBagConstraints();
		statusLabelConstraint = new GridBagConstraints();
		statusFieldConstraint = new GridBagConstraints();
		emailLabelConstraint = new GridBagConstraints();
		emailFieldConstraint = new GridBagConstraints();
		regdateLabelConstraint = new GridBagConstraints();
		regdateFieldConstraint = new GridBagConstraints();
	}
	
	
	private void initializeConstraints() {
		avatarButtonConstraint.gridx = 0;
		avatarButtonConstraint.gridy = 0;
		avatarButtonConstraint.weightx = 1;
		avatarButtonConstraint.weighty = 1;
		avatarButtonConstraint.anchor = GridBagConstraints.NORTHWEST;
		avatarButtonConstraint.insets = new Insets(0,20,5,0);
		
		usernameLabelConstraint.gridx = 1;
		usernameLabelConstraint.gridy = 0;
		usernameLabelConstraint.weightx = 1;
		usernameLabelConstraint.weighty = 1;
		usernameLabelConstraint.anchor = GridBagConstraints.NORTHWEST;
		usernameLabelConstraint.insets = new Insets(0,5,5,0);
		
		usernameFieldConstraint.gridx = 1;
		usernameFieldConstraint.gridy = 0;
		usernameFieldConstraint.weightx = 1;
		usernameFieldConstraint.weighty = 1;
		usernameFieldConstraint.anchor = GridBagConstraints.NORTHWEST;
		usernameFieldConstraint.insets = new Insets(20,5,5,0);
		
		nicknameLabelConstraint.gridx = 0;
		nicknameLabelConstraint.gridy = 1;
		nicknameLabelConstraint.weightx = 1;
		nicknameLabelConstraint.weighty = 1;
		nicknameLabelConstraint.anchor = GridBagConstraints.NORTHWEST;
		nicknameLabelConstraint.insets = new Insets(0,20,5,0);
		
		nicknameFieldConstraint.gridx = 1;
		nicknameFieldConstraint.gridy = 1;
		nicknameFieldConstraint.weightx = 1;
		nicknameFieldConstraint.weighty = 1;
		nicknameFieldConstraint.anchor = GridBagConstraints.NORTHWEST;
		nicknameFieldConstraint.insets = new Insets(0,5,5,0);
		
		statusLabelConstraint.gridx = 0;
		statusLabelConstraint.gridy = 2;
		statusLabelConstraint.weightx = 1;
		statusLabelConstraint.weighty = 1;
		statusLabelConstraint.anchor = GridBagConstraints.NORTHWEST;
		statusLabelConstraint.insets = new Insets(0,20,5,0);
		
		statusFieldConstraint.gridx = 1;
		statusFieldConstraint.gridy = 2;
		statusFieldConstraint.weightx = 1;
		statusFieldConstraint.weighty = 1;
		statusFieldConstraint.anchor = GridBagConstraints.NORTHWEST;
		statusFieldConstraint.insets = new Insets(0,5,5,0);
		
		emailLabelConstraint.gridx = 0;
		emailLabelConstraint.gridy = 3;
		emailLabelConstraint.weightx = 1;
		emailLabelConstraint.weighty = 1;
		emailLabelConstraint.anchor = GridBagConstraints.NORTHWEST;
		emailLabelConstraint.insets = new Insets(0,20,5,0);
		
		emailFieldConstraint.gridx = 1;
		emailFieldConstraint.gridy = 3;
		emailFieldConstraint.weightx = 1;
		emailFieldConstraint.weighty = 1;
		emailFieldConstraint.anchor = GridBagConstraints.NORTHWEST;
		emailFieldConstraint.insets = new Insets(0,5,5,0);
		
		regdateLabelConstraint.gridx = 0;
		regdateLabelConstraint.gridy = 4;
		regdateLabelConstraint.weightx = 1;
		regdateLabelConstraint.weighty = 1;
		regdateLabelConstraint.anchor = GridBagConstraints.NORTHWEST;
		regdateLabelConstraint.insets = new Insets(0,20,5,0);
		
		regdateFieldConstraint.gridx = 1;
		regdateFieldConstraint.gridy = 4;
		regdateFieldConstraint.weightx = 1;
		regdateFieldConstraint.weighty = 1;
		regdateFieldConstraint.anchor = GridBagConstraints.NORTHWEST;
		regdateFieldConstraint.insets = new Insets(0,5,5,0);
	}
	
	
	
	// Metode koje inicijalizuju komponente dialoga.
	
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			
			jContentPane.setLayout(new BorderLayout());
			
			jContentPane.add(getDetailsPanel(), BorderLayout.CENTER);
			jContentPane.add(getButtonPanel(), BorderLayout.SOUTH);
		}
		
		return jContentPane;
	}


	private JButton getAvatarButton() {
		if (avatarButton == null) {
			avatarButton = new JButton();
			avatarButton.setPreferredSize(new Dimension(50,50));
			avatarButton.setMinimumSize(new Dimension(50,50));
			gridBag.setConstraints(avatarButton, avatarButtonConstraint);
		}
		return avatarButton;
	}


	private JTextField getEmailField() {
		if (emailField == null) {
			emailField = new JTextField();
			emailField.setPreferredSize(fieldDimension);
			emailField.setEditable(false);
			emailField.setFont(font);
			emailField.setBackground(Color.WHITE);
			gridBag.setConstraints(emailField, emailFieldConstraint);
		}
		return emailField;
	}


	private JLabel getEmailLabel() {
		if (emailLabel == null) {
			emailLabel = new JLabel("Email:");
			gridBag.setConstraints(emailLabel, emailLabelConstraint);
		}
		return emailLabel;
	}


	private JTextField getNicknameField() {
		if (nicknameField == null) {
			nicknameField = new JTextField();
			nicknameField.setPreferredSize(fieldDimension);
			nicknameField.setEditable(false);
			nicknameField.setFont(font);
			nicknameField.setBackground(Color.WHITE);
			gridBag.setConstraints(nicknameField, nicknameFieldConstraint);
		}
		return nicknameField;
	}


	private JLabel getNicknameLabel() {
		if (nicknameLabel == null) {
			nicknameLabel = new JLabel("Nickname:");
			gridBag.setConstraints(nicknameLabel, nicknameLabelConstraint);
		}
		return nicknameLabel;
	}


	private JTextField getRegdateField() {
		if (regdateField == null) {
			regdateField = new JTextField();
			regdateField.setPreferredSize(fieldDimension);
			regdateField.setEditable(false);
			regdateField.setFont(font);
			regdateField.setBackground(Color.WHITE);
			gridBag.setConstraints(regdateField, regdateFieldConstraint);
		}
		return regdateField;
	}


	private JLabel getRegdateLabel() {
		if (regdateLabel == null) {
			regdateLabel = new JLabel("Reg.date:");
			gridBag.setConstraints(regdateLabel, regdateLabelConstraint);
		}
		return regdateLabel;
	}


	private JTextField getStatusField() {
		if (statusField == null) {
			statusField = new JTextField();
			statusField.setPreferredSize(fieldDimension);
			statusField.setEditable(false);
			statusField.setFont(font);
			statusField.setBackground(Color.WHITE);
			gridBag.setConstraints(statusField, statusFieldConstraint);
		}
		return statusField;
	}


	private JLabel getStatusLabel() {
		if (statusLabel == null) {
			statusLabel = new JLabel("Status:");
			gridBag.setConstraints(statusLabel, statusLabelConstraint);
		}
		return statusLabel;
	}


	private JTextField getUsernameField() {
		if (usernameField == null) {
			usernameField = new JTextField();
			usernameField.setPreferredSize(fieldDimension);
			usernameField.setEditable(false);
			usernameField.setFont(font);
			usernameField.setBackground(new Color(238,232,170));
			gridBag.setConstraints(usernameField, usernameFieldConstraint);
		}
		return usernameField;
	}


	private JLabel getUsernameLabel() {
		if (usernameLabel == null) {
			usernameLabel = new JLabel("Username:");
			gridBag.setConstraints(usernameLabel, usernameLabelConstraint);
		}
		return usernameLabel;
	}
	
	
	private void setValues() {
		avatarButton.setIcon(ImageCreator.createImageIcon(UserDetailsDialog.class, "/avatars/" + user.getAvatar() + ".png"));
		usernameField.setText(user.getUsername());
		nicknameField.setText(user.getNickname());
		statusField.setText(STATUS_NAMES[user.getStatus()]);
		emailField.setText(user.getEmail());
		regdateField.setText(user.getRegDate());
	}
	
	
	private void setValues(ClientUser user) {
		avatarButton.setIcon(ImageCreator.createImageIcon(UserDetailsDialog.class, "/avatars/" + user.getAvatar() + ".png"));
		usernameField.setText(user.getUsername());
		nicknameField.setText(user.getNickname());
		statusField.setText(STATUS_NAMES[user.getStatus()]);
		emailField.setText(user.getEmail());
		regdateField.setText(user.getRegDate());
	}
	
	
	private JPanel getDetailsPanel() {
		if (detailsPanel == null) {
			detailsPanel = new JPanel();
			detailsPanel.setLayout(gridBag);
			
			TitledBorder titledBorder = BorderFactory.createTitledBorder(BorderFactory.
					createEtchedBorder(EtchedBorder.RAISED),
					"User Details", javax.swing.border.TitledBorder.CENTER,
					javax.swing.border.TitledBorder.DEFAULT_POSITION,
					null, null);
			
			detailsPanel.setBorder(titledBorder);
			
			detailsPanel.add(getAvatarButton());
			detailsPanel.add(getUsernameLabel());
			detailsPanel.add(getUsernameField());
			detailsPanel.add(getNicknameLabel());
			detailsPanel.add(getNicknameField());
			detailsPanel.add(getStatusLabel());
			detailsPanel.add(getStatusField());
			detailsPanel.add(getEmailLabel());
			detailsPanel.add(getEmailField());
			detailsPanel.add(getRegdateLabel());
			detailsPanel.add(getRegdateField());
		}
		
		return detailsPanel;
	}
	
	
	private JPanel getButtonPanel() {
		if (buttonPanel == null) {
			buttonPanel = new JPanel();
			FlowLayout bpFlow = new FlowLayout(FlowLayout.CENTER);
			buttonPanel.setLayout(bpFlow);
			
			buttonPanel.add(getOKButton());
		}
		
		return buttonPanel;
	}
	
	private JButton getOKButton() {
		if (okButton == null) {
			okButton = new JButton("OK");
			okButton.setPreferredSize(new Dimension(80,25));
			okButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					UserDetailsDialog.this.dispose();
				}
			});
		}
		
		return okButton;
	}
	
}
